package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Member;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {


    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

   /**
    * 运营数据统计
    reportDate:null,//报告日期
    todayNewMember :0,//今日新增会员数
    totalMember :0,//总会员数
    thisWeekNewMember :0,//本周新增会员数
    thisMonthNewMember :0,//本月新增会员数
    todayOrderNumber :0,//今日预约数
    todayVisitsNumber :0,//今日到诊数
    thisWeekOrderNumber :0,//本周预约数
    thisWeekVisitsNumber :0,//本周到诊数
    thisMonthOrderNumber :0,//本月预约数
    thisMonthVisitsNumber :0,//本月到诊数
    hotSetmeal ://显示4个热门套餐
    */
    @Override
    public Map getBusinessReport() throws Exception {

        //今日日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //本月一号日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //todayNewMember :今日新增会员数
        Integer todayNewMember = memberMapper.findTodayNewMember(today);

        //totalMember :总会员数
        Integer totalMember = memberMapper.findTotalMember();

        //thisWeekNewMember:本周新增会员数
        Integer thisWeekNewMember = memberMapper.findMemberCountAfterOrderDate(thisWeekMonday);
        //thisMonthNewMember :本月新增会员数
        Integer thisMonthNewMember = memberMapper.findMemberCountAfterOrderDate(firstDay4ThisMonth);

        //todayOrderNumber :今日预约数
        Integer todayOrderNumber = orderMapper.findTodayOrderNumber(today);

        //todayVisitsNumber :今日到诊数
        Integer todayVisitsNumber = orderMapper.findTodayVisitsNumber(today);

        //thisWeekOrderNumber :本周预约数
        Integer thisWeekOrderNumber = orderMapper.findOrderNumberAfterDate(thisWeekMonday);

        //thisMonthOrderNumber :本月预约数
        Integer thisMonthOrderNumber = orderMapper.findOrderNumberAfterDate(firstDay4ThisMonth);

        //thisWeekVisitsNumber :本周到诊数
        Integer thisWeekVisitsNumber = orderMapper.findVisitsNumberAfterDate(thisWeekMonday);
        //thisMonthVisitsNumber :本月到诊数
        Integer thisMonthVisitsNumber = orderMapper.findVisitsNumberAfterDate(firstDay4ThisMonth);

        //查询热门套餐
        List<Map> hotSetmeal = orderMapper.findHotSetmeal();

        //将查询出来的数据全部封装到map
        Map<String,Object> result = new HashMap();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);

        return result;
    }
    /**
     * 查询会员性别比例及年龄比例
     *
     * @return
     */
    @Override
    public Map getSexAndAgeReport() throws Exception {
        //男会员数
        Integer sexBoy = memberMapper.findCountByBoy();
        //女会员数
        Integer sexGirl = memberMapper.findCountByGirl();

        //性别未知的会员数
        Integer sexUnknown = memberMapper.findAllCount() - sexBoy - sexGirl;

        //男会员统计
        HashMap<String, Object> boy = new HashMap<>();
        boy.put("value", sexBoy);
        boy.put("name", "男");
        //女会员统计
        HashMap<String, Object> girl = new HashMap<>();
        girl.put("value", sexGirl);
        girl.put("name", "女");
        //未知性别会员统计
        HashMap<String, Object> unknown = new HashMap<>();
        unknown.put("value", sexUnknown);
        unknown.put("name", "未知");
        //封装一个list作为性别报表数据
        List<Map> sex = new ArrayList<>();
        sex.add(boy);
        sex.add(girl);
        sex.add(unknown);

        //今日日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //0-18岁会员数量
        Integer count0_18 = memberMapper.findMemberCountByBirthday(DateUtils.getNYearDate(18),today);
        //18-30岁会员数量
        Integer count18_30 = memberMapper.findMemberCountByBirthday(DateUtils.getNYearDate(30),DateUtils.getNYearDate(18));
        //30-45岁会员数量
        Integer count30_45 = memberMapper.findMemberCountByBirthday(DateUtils.getNYearDate(45),DateUtils.getNYearDate(30));
        //45岁以上会员数量
        Integer count45 = memberMapper.findMemberCountByBirthdayMax(DateUtils.getNYearDate(45));

        HashMap<String, Object> count1 = new HashMap<>();
        count1.put("value", count0_18);
        count1.put("name", "0-18岁");
        HashMap<String, Object> count19 = new HashMap<>();
        count19.put("value", count18_30);
        count19.put("name", "18-30岁");
        HashMap<String, Object> count31 = new HashMap<>();
        count31.put("value", count30_45);
        count31.put("name", "30-45岁");
        HashMap<String, Object> count46 = new HashMap<>();
        count46.put("value", count45);
        count46.put("name", "45岁以上");
        //封装到一个list
        List<Map> age = new ArrayList<>();
        age.add(count1);
        age.add(count19);
        age.add(count31);
        age.add(count46);
        //封装数据并返回
        Map<String, List> result = new HashMap<>();
        result.put("sex",sex);
        result.put("age",age);

        return result;
    }
}
